package es.farfuteam.vncpp.controller;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;

import com.test.tablayout.testapplication.R;

import es.farfuteam.vncpp.model.sql.Connection;
import es.farfuteam.vncpp.model.sql.ConnectionSQLite;

import static android.text.TextUtils.isEmpty;

public class QMXConnectActivity extends Activity {
    /**
     * connection name
     */
    private String connectionName;
    /**
     * connection ip
     */
    private String IP;
    /**
     * connection port
     */
    private String PORT;
    /**
     * connection psw
     */
    private String PSW;

    //    public enum QualityArray {
    //        SuperHigh, High, Medium, Low
    //    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qmxconnect);

        connectionName = getIntent().getStringExtra("connectionName");
        IP = this.getIntent().getStringExtra("ip");
        PORT = this.getIntent().getStringExtra("port");
        PSW = this.getIntent().getStringExtra("psw");

        Log.i("QMXConnectActivity", connectionName + ":" + IP + ":" + PORT + ":" + PSW);

        createNewConnection(connectionName, IP, PORT, PSW);
        iniCanvasActivity(IP, PORT, PSW);
    }

    /**
     * @brief Creates and connects to the new connection
     * @details Creates and connects to the new connection
     */
    private void createNewConnection(String connectionName, String IP, String PORT, String PSW) {

        NewConnectionActivity newConnectionActivity = new NewConnectionActivity();

        if (!isEmpty(connectionName) && !isEmpty(IP) && !isEmpty(PORT)) {
            Connection c = new Connection(connectionName, IP, PORT, PSW, false, newConnectionActivity.getColor_format());
            //se anade el usuario a la base de datos
            ConnectionSQLite dataBase = ConnectionSQLite.getInstance(this);
            dataBase.newUser(c);
            Log.i("createNewConnection", "createNewConnection------------------------");

        } else {
            //Dialogo alerta
            //            showDialog(1);

        }
    }

    /**
     * @brief Method that initialized the Canvas Activity
     * @details Method that initialized the Canvas Activity
     */
    private void iniCanvasActivity(String IP, String PORT, String PSW) {

        Intent canvasActivity = new Intent(this, CanvasActivity.class);

        canvasActivity.putExtra("ip", IP);
        canvasActivity.putExtra("port", PORT);
        canvasActivity.putExtra("psw", PSW);
        canvasActivity.putExtra("color", "Medium");

        //Aquí veo el tipo de conexión, para usar un tipo de compresión de imagen u otro
        if (checkConnectivity()) {
            Log.i("checkConnectivity", checkConnectivity() + "");
            canvasActivity.putExtra("wifi", isWifiConnectivityType());

            startActivity(canvasActivity);

            //se finaliza activity
            //            finish();

        } else {
            //dialogo alerta No conexion habilitada
            showDialog(2);
        }
    }

    /**
     * @return True if the connectivity exists, false in another case.
     * @brief Checks the connectivity of the terminal
     * @details Checks the connectivity of the terminal
     */
    private boolean checkConnectivity() {
        boolean enabled = true;

        ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();

        if ((info == null || !info.isConnected() || !info.isAvailable())) {
            enabled = false;
        }
        return enabled;
    }

    /**
     * @return True if the connectivity type is Wifi, false in another case.
     * @brief Checks the connectivity type of the terminal
     * @details Checks the connectivity type of the terminal
     */
    //devuelve true si es conexion wifi, false en caso contrario
    private boolean isWifiConnectivityType() {
        ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();

        String connectionType = info.getTypeName();

        if (connectionType.equalsIgnoreCase("wifi")) {
            return true;
        } else {
            //3g u otro tipo
            return false;
        }
    }
}
